
var requests = require('../../requests/request.js');
var utils = require('../../utils/util.js');
// viewresult.js
Page({
  data: {
    isZan:0,    //是否点赞
    testId:'',         //测试题目id
    resultId:'',        //测试结果id
    session3rd:'',
    resultData:null
  },
  onLoad: function (options) {
    const that = this;
    const resultId = options.resultId;
    this.setData({ 
      resultId: resultId
    })
    wx.getStorage({
      key: '3rd_session',
      success: function(res) {              //如果缓存中有session 则直接请求测试结果数据
        console.log('有session',res)
        that.setData({
          session3rd:res.data
        })
        wx.showToast({
          title: '加载中...',
          icon: 'loading',
          duration: 10000
        })
        requests.requestViewResult(         //请求结果数据
          {
            result_id: resultId,
            session: res.data
          }, (res) => {
            setTimeout(function () {
              wx.hideLoading()
            }, 1000)
            that.setData({
              resultData: res,
              isZan: res.is_like,
              testId: res.test_id
            });
          }, () => {
            //请求失败
          }, () => {
            //请求完成
          }
        )

      },
      fail:function(){                 //如果缓存中没有session  则登录
        console.log('没有session')
        wxLogin.call(that) //登录
      }
    })
    
  },
  retest:function(e){    //重新测试
    const testId = this.data.testId;
    wx.redirectTo({
      url: `../starttest/starttest?id=${testId}`
    })
  },
  moreTest:function(){  //更多测试  返回首页
    wx.switchTab({
      url: '../index/index'
    })
  },
  zan:function(){       //点赞
    const that = this;
    const testId = this.data.testId;
    const session = this.data.session3rd;
    var reData = this.data.resultData;
    requests.requestDianZan(
      {
        test_id: testId,
        session: session
      },(res) => {
        // console.log(res);
        if (res.is_like) {              //点赞
          reData.like_num ++;
          that.setData({ 
            isZan:1,
            resultData: reData
          });               
        } else {                        //取消点赞
          reData.like_num --;           
          that.setData({ 
            isZan: 0,
            resultData: reData
          });              
        }
      },() => {
        //请求失败
      },() => {
        //请求完成
      }
    )
  },
  onShareAppMessage: function () {    //分享测试结果
    const resultId = this.data.resultId;
    const title = this.data.resultData.test_title;
    return {
      title: title,
      path: `/pages/viewresult/viewresult?resultId=${resultId}`,
      success: function (res) {
        // 分享成功

      },
      fail: function (res) {
        // 分享失败
      }
    }
  }
})

//登录
function wxLogin(func) {
  // console.log(this)
  //调用登录接口
  //1.小程序调用wx.login得到code.
  var _this = this;
  wx.login({
    success: function (res) {
      var code = res['code'];
      //   console.log(code)
      //2.小程序调用wx.getUserInfo得到rawData, signatrue, encryptData.
      wx.getUserInfo({
        success: function (info) {
          // console.log(info);
          var rawData = info['rawData'];
          var signature = info['signature'];
          var encryptData = info['encryptData'];
          var encryptedData = info['encryptedData']; //注意是encryptedData不是encryptData...坑啊
          var iv = info['iv'];
          wx.showToast({
            title: '加载中...',
            icon: 'loading',
            duration: 10000
          })
          //3.小程序调用server获取token接口, 传入code, rawData, signature, encryptData.
          wx.request({
            url: 'https://im.17link.cc/PsyFace/login',
            data: {
              "code": code,
              "rawData": rawData,
              "signature": signature,
              "encryptData": encryptData,
              'iv': iv,
              'encryptedData': encryptedData
            },
            success: function (res) {
              // console.log(res)
              if (res.statusCode != 200) {
                wx.showModal({
                  title: '登录失败'
                });
              } else {
                var session = res.data.session;
                var msg = {};
                msg.nick_name = res.data.nick_name;
                msg.avatar_url = res.data.avatar_url;
                wx.setStorage({    //session3rd存入微信缓存
                  key: "3rd_session",
                  data: session,
                  success: function () {
                    // console.log('session3rd存入缓存')
                  }
                })
                wx.setStorage({    //session3rd存入微信缓存
                  key: "msg",
                  data: msg,
                  success: function () {
                    // console.log('个人信息存入缓存')
                  }
                })
                //请求结果测试结果数据
                _this.setData({
                  session3rd: session
                })
                const _that = _this;   //_this指page
                const resultId = _this.data.resultId;
                requests.requestViewResult(         //请求结果数据
                  {
                    result_id: resultId,
                    session: session
                  }, (res) => {
                    setTimeout(function () {
                      wx.hideLoading()
                    }, 1000)
                    _that.setData({
                      resultData: res,
                      isZan: res.is_like,
                      testId: res.test_id
                    });
                  }, () => {
                    //请求失败
                  }, () => {
                    //请求完成
                  }
                )

              }
              typeof func == "function" && func(res.data);
            }
          });
        }
      });
    }
  });
}